package com.lg.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lg.cms.entity.Article;
import com.lg.cms.entity.Category;
import com.lg.cms.entity.Rotation;
import com.lg.cms.mapper.ArticleMapper;
import com.lg.cms.mapper.CategoryMapper;
import com.lg.cms.mapper.RotationMapper;
import com.lg.cms.service.CategoryService;
import com.lg.cms.service.RotationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lg.cms.vo.admin.RotationArticleVO;
import com.lg.cms.vo.admin.SuperRotationVO;
import com.lg.cms.vo.news.RotationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 首页轮播图表 服务实现类
 * </p>
 *
 * @author lg
 * @since 2020-09-21
 */
@Service
public class RotationServiceImpl extends ServiceImpl<RotationMapper, Rotation> implements RotationService {

    @Autowired
    RotationMapper rotationMapper;
    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<RotationVO> getList() {
        List<RotationVO> list = new ArrayList<>();
        QueryWrapper<Rotation> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        List<Rotation> rotationList = rotationMapper.selectList(wrapper);
        for(Rotation r: rotationList){
            list.add(this.toRotationVO(r));
        }
        return list;
    }

    @Override
    public RotationVO toRotationVO(Rotation rotation) {
        RotationVO vo = new RotationVO();
        Article article = articleMapper.selectById(rotation.getArticleId());
        vo.setCoverImage(rotation.getCoverImage());
        vo.setArticle(article);
        vo.setTitle(article.getTitle());
        vo.setUrl("/info_article/" + article.getId());
        return vo;
    }

    @Override
    public RotationArticleVO toRotationArticleVO(Article article) {
        RotationArticleVO vo = new RotationArticleVO();
        vo.setId(article.getId());
        vo.setCategoryId(article.getCategoryId());
        vo.setCoverImage(article.getCoverImage());
        vo.setDescription(article.getDescription());
        vo.setTitle(article.getTitle());
        Category category = categoryMapper.selectById(article.getCategoryId());
        if(category != null){
            vo.setCategoryName(category.getName());
        }else{
            vo.setCategoryName("  ");
        }
        return vo;
    }

    @Override
    public SuperRotationVO toSuperRotationVO(Rotation rotation) {
        SuperRotationVO vo = new SuperRotationVO();
        vo.setId(rotation.getId());
        vo.setCoverImage(rotation.getCoverImage());
        vo.setCreateTime(rotation.getCreateTime());
        vo.setSort(rotation.getSort());
        vo.setUpdateTime(rotation.getUpdateTime());
        Article article = articleMapper.selectById(rotation.getArticleId());
        vo.setArticleVO(this.toRotationArticleVO(article));
        return vo;
    }
}
